<template>
  <div class="newTest">
    <div class="title" style="border-bottom: 1px solid #eee">因子设置</div>
    <div class="searchBox">
      <el-form
        label-position="top"
        inline
        :model="searchForm"
        size="mini"
        style="max-height: 100px"
      >
        <el-form-item>
          <p slot="label" class="miniLabel">因子选择</p>
          <el-select v-model="searchForm.name" placeholder="请选择因子">
            <el-option
              v-for="item in factorList"
              :key="item.name"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <p slot="label" class="miniLabel">站点选择</p>
          <el-select
            v-model="searchForm.stationSystem"
            placeholder="请选择站点"
          >
            <el-option
              v-for="(item, value) in stationSystemList"
              :key="item"
              :label="value"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <p class="searchBtns">
            <el-button type="success" class="greenBtn" @click="searchClick()"
              >确定</el-button
            >
            <el-button
              type="success"
              class="greenBtn"
              @click="addBtnClick(false)"
              >新增</el-button
            >
          </p>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <div v-loading="hisLoading">
      <mergeTable
        :tableData="tableList"
        :tableConfig="tableConfig"
        :attributes="{
          border: true,
          headerCellStyle: {
            backgroundColor: 'transparent',
          },
          cellStyle: {
            height: '50px',
            backgroundColor: 'transparent',
          },
          headerRowStyle: {
            backgroundColor: 'transparent',
          },
          rowStyle: {
            backgroundColor: 'transparent',
          },
        }"
      >
        <el-table-column
          slot="operation"
          align="center"
          label="操作"
          width="140"
        >
          <template slot-scope="{ row }">
            <div class="options-button">
              <div class="options-button-item">
                <el-button size="mini" type="primary" @click="addBtnClick(row)"
                  >修改</el-button
                >
              </div>
              <div>
                <el-button size="mini" type="primary" @click="delBtnClick(row)"
                  >删除</el-button
                >
              </div>
            </div>
          </template>
        </el-table-column>
      </mergeTable>
    </div>

    <v-paging @setCurrent="setPage" @setCurren="setNum" :total="totalpage" />
    <!-- 新增弹出框 -->
    <el-dialog
      :title="dilogTitle === 'add' ? '新增' : '修改'"
      :visible.sync="dilogVisible"
      width="50%"
      class="abow_dialog dialog"
      label-width="140px"
      :modal-append-to-body="false"
    >
      <el-form ref="form" :model="form" label-width="140px" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="监测因子" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入监测因子"
                class="form-input"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="因子限值" prop="factorLimit">
              <el-input
                v-model="form.factorLimit"
                placeholder="请输入因子限值"
                class="form-input"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="因子最小阈值" prop="minthreshold">
              <el-input
                v-model="form.minthreshold"
                placeholder="请输入因子最小阈值"
                class="form-input"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="因子最大阈值" prop="maxthreshold">
              <el-input
                v-model="form.maxthreshold"
                placeholder="请输入因子最大阈值"
                class="form-input"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据清洗最小区间" prop="factorMinclean">
              <el-input
                v-model="form.factorMinclean"
                placeholder="请输入数据清洗最小区间"
                class="form-input"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据清洗最大区间" prop="factorMaxclean">
              <el-input
                v-model="form.factorMaxclean"
                placeholder="请输入数据清洗最大区间"
                class="form-input"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警最小阈值" prop="minWarning">
              <el-input
                v-model="form.minWarning"
                placeholder="请输入预警最小阈值"
                class="form-input"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警最大阈值" prop="maxWarning">
              <el-input
                v-model="form.maxWarning"
                placeholder="请输入预警最大阈值"
                class="form-input"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最大浮动值" prop="maximumFloat">
              <el-input
                v-model="form.maximumFloat"
                placeholder="请输入最大浮动值"
                class="form-input"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最小浮动值" prop="minimumFloat">
              <el-input
                v-model="form.minimumFloat"
                placeholder="请输入最小浮动值"
                class="form-input"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="因子等级" prop="factorType">
              <el-input
                v-model="form.factorType"
                placeholder="请输入因子等级"
                class="form-input"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="因子权重" prop="factorWeight">
              <el-input
                v-model="form.factorWeight"
                placeholder="请输入因子权重"
                class="form-input"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="系统编号" prop="statisticType">
              <el-select
                v-model="form.stationSystem"
                placeholder="请选择系统编号"
              >
                <el-option
                  v-for="(item, index) in stationSystemList"
                  :key="index"
                  :label="index"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer" id="dialog-footer">
        <el-button type="primary" @click="submitAction(dilogTitle)"
          >提交</el-button
        >
      </span>
    </el-dialog>
    <!-- 修改弹出框 -->
    <!-- <el-dialog
      :title="dilogTitle"
      :visible.sync="modifyTips"
      width="50%"
      class="abow_dialog dialog"
      label-width="140px"
      :modal-append-to-body="false"
    >
      <el-form ref="form" :model="form" label-width="140px" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="监测因子" prop="name">
              <el-input v-model="form.name" placeholder="请输入监测因子" class="form-input"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="因子限值" prop="factorLimit">
              <el-input v-model="form.factorLimit" placeholder="请输入因子限值" class="form-input"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="因子最小阈值" prop="minthreshold">
              <el-input v-model="form.minthreshold" placeholder="请输入因子最小阈值" class="form-input"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="因子最大阈值" prop="maxthreshold">
              <el-input v-model="form.maxthreshold" placeholder="请输入因子最大阈值" class="form-input"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据清洗最小区间" prop="factorMinclean">
              <el-input v-model="form.factorMinclean" placeholder="请输入数据清洗最小区间" class="form-input"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据清洗最大区间" prop="factorMaxclean">
              <el-input v-model="form.factorMaxclean" placeholder="请输入数据清洗最大区间" class="form-input"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警最小阈值" prop="minWarning">
              <el-input v-model="form.minWarning" placeholder="请输入预警最小阈值" class="form-input"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警最大阈值" prop="maxWarning">
              <el-input v-model="form.maxWarning" placeholder="请输入预警最大阈值" class="form-input"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="因子等级" prop="factorType">
              <el-input v-model="form.factorType" placeholder="请输入因子等级" class="form-input"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer" id="dialog-footer">
        <el-button type="primary" @click="submitAction('update')">提交</el-button>
      </span>
    </el-dialog>-->
  </div>
</template>
<script>
import {
  factorAll,
  factorAdd,
  factorDelete,
  factorList,
  factorUpdate,
  stationSystem,
} from "@/http/api.js";
export default {
  name: "factorSetting",
  data() {
    return {
      searchForm: {
        name: null,
        stationSystem: null,
        pageNum: 1,
        pageSize: 12,
      },
      hisLoading: false,
      factorList: [],
      totalpage: 0,
      form: {
        name: "",
        factorLimit: null,
        minthreshold: null,
        maxthreshold: null,
        factorMinclean: null,
        factorMaxclean: null,
        minWarning: null,
        maxWarning: null,
        factorType: null,
        factorWeight: null,
        minimumFloat: null,
        maximumFloat: null,
        stationSystem: "",
      },
      dilogTitle: "add",
      dilogVisible: false,
      tableList: [],
      stationSystemList: {},
      tableConfig: [
        { prop: "name", label: "监测因子", align: "center" },
        { prop: "factorLimit", label: "因子限值", align: "center" },
        { prop: "minthreshold", label: "因子最小阈值", align: "center" },
        { prop: "maxthreshold", label: "因子最大阈值", align: "center" },
        { prop: "factorMinclean", label: "数据清洗最小区间", align: "center" },
        { prop: "factorMaxclean", label: "数据清洗最大区间", align: "center" },
        { prop: "minWarning", label: "预警最小阈值", align: "center" },
        { prop: "maxWarning", label: "预警最大阈值", align: "center" },
        { prop: "factorWeight", label: "因子权重", align: "center" },
        { prop: "minimumFloat", label: "最小浮动值", align: "center" },
        { prop: "maximumFloat", label: "最大浮动值", align: "center" },
        { prop: "stationSystem", label: "系统编号", align: "center" },
        { prop: "operation", slot: "operation" },
      ],
      rules: {
        name: [{ required: true, message: "请输入监测因子", trigger: "blur" }],
        factorLimit: [
          { required: true, message: "请输入因子限制", trigger: "blur" },
        ],
        minthreshold: [
          { required: true, message: "请输入因子最小阈值", trigger: "blur" },
        ],
        maxthreshold: [
          { required: true, message: "请输入因子最大阈值", trigger: "blur" },
        ],
        factorMinclean: [
          {
            required: true,
            message: "请输入数据清洗最小区间",
            trigger: "blur",
          },
        ],
        factorMaxclean: [
          {
            required: true,
            message: "请输入数据清洗最大区间",
            trigger: "blur",
          },
        ],
        minWarning: [
          { required: true, message: "请输入预警最小阈值", trigger: "blur" },
        ],
        maxWarning: [
          { required: true, message: "请输入预警最大阈值人", trigger: "blur" },
        ],
        factorType: [
          { required: true, message: "请输入因子等级", trigger: "blur" },
        ],

        factorWeight: [
          { required: true, message: "请输入因子权重", trigger: "blur" },
        ],
        minimumFloat: [
          { required: true, message: "请输入最小浮动值", trigger: "blur" },
        ],
        maximumFloat: [
          { required: true, message: "请输入最大浮动值", trigger: "blur" },
        ],
        stationSystem: [
          { required: true, message: "请选择系统编号", trigger: "change" },
        ],
      },
    };
  },
  created() {
    // 查询所有监测因子
    factorAll().then((res) => {
      this.factorList = res.data;
    });
    this.getList();
    this.getStationSystem();
  },
  methods: {
    // 查询监测因子
    getList() {
      this.hisLoading = true;
      factorList(this.searchForm).then((res) => {
        this.tableList = res.data.data;
        this.totalpage = res.data.total;
        if (res.data.data === 0 && this.totalpage > 0) {
          this.searchForm.pageNum--;
          factorList(this.searchForm).then((res) => {
            this.tableList = res.data.data;
            this.totalpage = res.data.total;
          });
        }
        this.hisLoading = false;
      });
    },
    //获取系统编号
    async getStationSystem() {
      let res = await stationSystem();
      this.stationSystemList = res.data;
    },
    //提交操作
    async submitAction(id) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (id === "add") {
            factorAdd(this.form).then((res) => {
              if (res) {
                this.getList();
                this.$notify({
                  title: "成功",
                  message: res.info,
                  type: "success",
                });
                this.$refs["form"].resetFields();
                this.dilogVisible = false;
              }
            });
          } else {
            factorUpdate(this.form).then((res) => {
              if (res) {
                this.getList();
                this.$notify({
                  title: "成功",
                  message: res.info,
                  type: "success",
                });
                this.dilogVisible = false;
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    // 搜索确定
    searchClick() {
      this.getList();
    },
    // 新增因子
    addBtnClick(scope) {
      if (scope === false) {
        this.dilogVisible = true;
        this.dilogTitle = "add";
        this.form.name = this.searchForm.name;
      } else {
        this.dilogVisible = true;
        this.dilogTitle = "update";
        Object.assign(this.form, scope);
      }
    },
    // 删除因子
    delBtnClick(row) {
      this.$confirm("确定删除监测因子-" + row.id, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          factorDelete({ id: row.id }).then((res) => {
            this.getList();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //设置条数
    setNum(val) {
      this.searchForm.pageNum = val;
      this.getList();
    },
    //设置页数
    setPage(val) {
      this.searchForm.pageSize = val;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
@media (max-width: 1700px) {
  .options-button {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    .options-button-item {
      margin-bottom: 10px;
    }
  }
}
.options-button {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
#dialog-footer {
  display: flex !important;
  justify-content: center;
}
.newTest .el-dialog .el-dialog__footer {
  display: flex !important;
  justify-content: center;
  color: red;
}
.newTest {
  background: #fff;
  min-height: 100%;
  padding: 0 20px 0 20px;
}
.newTest {
  .title {
    position: relative;
    left: 15px;
    line-height: 70px;
    font-size: 16px;
  }
  .title::before {
    content: "";
    position: absolute;
    left: -15px;
    width: 4px;
    height: 20px;
    top: 25px;
    background-color: #2399ff;
  }
}

.searchBox {
  padding: 20px 20px 0;
  margin-bottom: 20px;
  background-color: #fff;
  .miniLabel {
    line-height: 0;
  }
}

.searchBtns {
  margin-top: 19px;
}
.abow_dialog {
  display: flex;
  justify-content: center;
  align-items: Center;
  overflow: hidden;
}

.abow_dialog .el-dialog {
  position: absolute;
  left: 0;
  top: 54px;
  bottom: 0;
  right: 0;
  padding: 0;
  z-index: 1;
  overflow: hidden;
  overflow-y: auto;
}
</style>